<template>
  <div class="login-container">
    <div class="login-box">
      <el-form :model="loginForm" ref="formRef" label-width="80px" class="login-form" :rules="loginRules">
        <h2 class="form-title">用户登录</h2>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" ref="username" name="username" autocomplete="off"
            placeholder="请输入用户名" size="large" prefix-icon="User" />
        </el-form-item>
        <el-form-item label="密码" prop="userPwd">
          <el-input type="password" v-model="loginForm.userPwd" autocomplete="off" placeholder="请输入密码" size="large"
            prefix-icon="Lock" show-password />
        </el-form-item>
        <el-form-item class="button-group">
          <el-button type="primary" @click.native.prevent="login" size="large" :loading="loading">登录</el-button>
          <el-button type="success" @click="toRegister" size="large">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
/**
 * 登录页面组件
 * 提供用户登录界面和功能
 */
import { ref } from "vue"
import { useUserInfoStore } from '../../stores/userInfo';
import { User, Lock } from '@element-plus/icons-vue'
import type { FormInstance } from 'element-plus';
import { useRouter } from 'vue-router'

// 获取用户信息存储实例
const userInfoStore = useUserInfoStore()
// 获取路由实例
const router = useRouter()
// 表单引用
const formRef = ref<FormInstance>()
// 登录按钮加载状态
const loading = ref(false)

/**
 * 登录表单数据模型
 * 包含用户名和密码字段
 */
const loginForm = ref({
  username: "zhangsan",
  userPwd: "123456",
})

/**
 * 用户名校验函数
 * @param rule 校验规则对象
 * @param value 当前输入值
 * @param callback 回调函数，用于返回校验结果
 */
const validateUsername = (rule: any, value: any, callback: any) => {
  if (value.length < 4) {
    callback(new Error('用户名长度不能小于4位'))
  } else {
    callback()
  }
}

/**
 * 密码校验函数
 * @param rule 校验规则对象
 * @param value 当前输入值
 * @param callback 回调函数，用于返回校验结果
 */
const validatePassword = (rule: any, value: any, callback: any) => {
  if (value.length < 6) {
    callback(new Error('密码长度不能小于6位'))
  } else {
    callback()
  }
}

/**
 * 登录表单校验规则
 * 定义用户名和密码的校验规则
 */
const loginRules = {
  username: [{ required: true, validator: validateUsername }],
  userPwd: [{ required: true, trigger: 'blur', validator: validatePassword }]
}

/**
 * 登录处理函数
 * 执行表单校验并尝试登录
 */
const login = async () => {
  // 表单校验
  await formRef.value?.validate()
  loading.value = true
  try {
    // 调用用户信息存储中的登录方法
    await userInfoStore.login(loginForm.value)
    // 登录成功后跳转到头条新闻页面
    router.push({ name: "HeadlineNews" });
  } finally {
    // 无论登录成功与否都关闭加载状态
    loading.value = false
  }
}

/**
 * 跳转到注册页面
 * 点击注册按钮时调用此函数
 */
const toRegister = () => {
  router.push({ name: "Register" });
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.login-box {
  width: 450px;
  padding: 40px;
  background: white;
  border-radius: 10px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
}

.form-title {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
  font-size: 28px;
}

.button-group {
  display: flex;
  justify-content: space-between;
  margin-top: 30px;
}

:deep(.el-form-item__label) {
  font-weight: 500;
  color: #555;
}
</style>